package com.sskj.gocoin.v.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.component.DaggerUserDataComponent;
import com.sskj.gocoin.m.bean.AddressList;
import com.sskj.gocoin.p.activity.CashPresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.TipUtil;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * <pre>
 *     author : 李岩
 *     e-mail : 465357793@qq.com
 *     time   : 2018/04/03
 *     desc   : 选择提现地址
 *     version: 1.0
 * </pre>
 */
public class CashAddressListActivity extends BaseActivity<CashPresenter> {

    @BindView(R2.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R2.id.address_recyclerveiw)
    RecyclerView mAddressRecyclerveiw;
    private BaseAdapter<AddressList.DataBean> mAdapter;
    private boolean mIsCallBack;

    @Inject
    UserViewModel userViewModel;
    UserData userData;

    int assetId;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cash_address_list;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requestAddressList(assetId);
    }

    @Override
    public CashPresenter getPresenter() {
        return new CashPresenter();
    }

    @Override
    protected void initView() {

        assetId = getIntent().getIntExtra(Constants.INTENT_ASSET_ID, -1);
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
        });
        mIsCallBack = getIntent().getBooleanExtra(Constants.INTENT_CALL_BACK, false);
        mToolBar.setLeftButtonOnClickLinster(v -> finish());

        ClickUtil.click(mToolBar.mRightButton, () -> {
            startActivity(new Intent(mActivity, CashAddressAddActivity.class).putExtra(Constants.INTENT_ADDRESS_TYPE, "usdt"));
        });
        mAddressRecyclerveiw.setLayoutManager(new LinearLayoutManager(this));


        /**
         * 比特币提币 地址列表
         */
        mAdapter = new BaseAdapter<AddressList.DataBean>(R.layout.item_cash_address, null, mAddressRecyclerveiw) {

            @Override
            public void bind(ViewHolder holder, AddressList.DataBean item) {
                holder.setText(R.id.address_list_remark, TextUtils.isEmpty(item.getNotes()) ? App.INSTANCE.getString(R.string.strCashAddressListActivity203) : item.getNotes());
                holder.setText(R.id.address_list_content, item.getAddress());
                holder.setText(R.id.address_list_title, item.getCode());
                holder.setOnClickListener(R.id.item_address_layout, v -> {
                    if (mIsCallBack) {
                        Intent intent = new Intent();
                        intent.putExtra("address_url", item.getAddress());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
                holder.setOnClickListener(R.id.address_list_content_delete, v -> {
                    googleCheck(1, holder.getLayoutPosition(), item.getId()+"");
//                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
//                    builder.setTitle(App.INSTANCE.getString(R.string.strCashAddressListActivity205))
//                            .setMessage(App.INSTANCE.getString(R.string.strCashAddressListActivity206))
//                            .setPositiveButton(App.INSTANCE.getString(R.string.strCashAddressListActivity207), new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    dialogInterface.dismiss();
//                                    mPresenter.requestDelite(1, holder.getLayoutPosition(), item.getId());
//                                }
//                            })
//                            .setNegativeButton(App.INSTANCE.getString(R.string.strCashAddressListActivity208), new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    dialogInterface.dismiss();
//                                }
//                            });
//                    builder.show();
                });
            }
        };
    }


    public void onResponseCashAddressListSuccess(AddressList data) {
        mAdapter.setNewData(data.getData());
    }

    /**
     * @param position
     */
    public void onDeliteSuccess(int position) {
        mAdapter.remove(position);
    }

    /**
     * 谷歌验证
     */
    private void googleCheck(int type, int position, String id) {
       // if (userData.getConfig().getGoogle_verify() == 1) {
        if (false){
            TipUtil.googleCheckTip(this, (pwd) -> {
                mPresenter.requestGoogleCheck(pwd, type, position, id);
            });
        } else {
            //关闭之后不作处理
            googleCheckSuccess(type, position, id);
        }
    }

    /**
     * 谷歌验证通过
     */
    public void googleCheckSuccess(int type, int position, String id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle(App.INSTANCE.getString(R.string.strCashAddressListActivity205))
                .setMessage(App.INSTANCE.getString(R.string.strCashAddressListActivity206))
                .setPositiveButton(App.INSTANCE.getString(R.string.strCashAddressListActivity207), (dialogInterface, i) -> {
                    dialogInterface.dismiss();
                    mPresenter.requestDelite(type, position, id);
                })
                .setNegativeButton(App.INSTANCE.getString(R.string.strCashAddressListActivity208), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        builder.show();
    }
}
